﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Productions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Productions
{
    public class ProTemporaryWorkHoursServices : IProTemporaryWorkHoursServices
    {
        //异步方法
        public ProTemporaryWorkHoursServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }


        /// <summary>
        /// 插入单条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(ProTemporaryWorkHours model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }


        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete ProTemporaryWorkHours where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete ProTemporaryWorkHours where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }


        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<ProTemporaryWorkHours>> GetAllAsync()
        {
            string sql = "select * from ProTemporaryWorkHours";
            var list = await Db.QueryAllAsync<ProTemporaryWorkHours>(sql);
            return list;
        }


        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="WorkhoursType"></param>
        /// <param name="Workhours"></param>
        /// <param name="WorkerName"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ProTemporaryWorkHours>> GetAsync(int PageIndex, int PageSize, string WorkhoursType, string Workhours, string WorkerName)
        {
            string sql = "select * from ProTemporaryWorkHours where 1=1 ";
            if (!string.IsNullOrEmpty(WorkhoursType))
            {
                sql += " and WorkhoursType like @WorkhoursType ";
            }

            if (!string.IsNullOrEmpty(Workhours))
            {
                sql += " and Workhours like @Workhours";
            }

            if (!string.IsNullOrEmpty(WorkerName))
            {
                sql += " and WorkerName like @WorkerName";
            }

            var list = await Db.QueryAsync<ProTemporaryWorkHours>(sql, new { PageIndex, PageSize, WorkhoursType = $"%{WorkhoursType}%", Workhours = $"%{Workhours}%", WorkerName = $"%{WorkerName}%" });
            return list;
        }


        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <param name="WorkhoursType"></param>
        /// <param name="Workhours"></param>
        /// <param name="WorkerName"></param>
        /// <returns></returns>
        public async Task<int> GetCountAsync(string WorkhoursType, string Workhours, string WorkerName)
        {
            string sql = "select * from ProTemporaryWorkHours where 1=1 ";
            if (!string.IsNullOrEmpty(WorkhoursType))
            {
                sql += " and WorkhoursType like @WorkhoursType ";
            }

            if (!string.IsNullOrEmpty(Workhours))
            {
                sql += " and Workhours like @Workhours";
            }

            if (!string.IsNullOrEmpty(WorkerName))
            {
                sql += " and WorkerName like @WorkerName";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { WorkhoursType = $"%{WorkhoursType}%", Workhours = $"%{Workhours}%", WorkerName = $"%{WorkerName}%" });
            return count;

        }

        public Task<int> GetCountAsync(string Workhours, string WorkerName)
        {
            throw new NotImplementedException();
        }

        public async Task<IEnumerable<Employees>> GetEmployeesAllAsync(string PersonCode, string Name)
        {
            string sql = "select * from Employees where 1=1 ";
            if (!string.IsNullOrEmpty(PersonCode))
            {
                sql += " and PersonCode like @PersonCode ";
            }

            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name";
            }


            var list = await Db.QueryAsync<Employees>(sql, new { PersonCode = $"%{PersonCode}%", Name = $"%{Name}%"});
            return list;
        }

        public async Task<IEnumerable<ProTemporaryWorkHours>> GetWorkhours(string Workhours)
        {
            string sql = "select sum(cast(Workhours as decimal)) from ProTemporaryWorkHours where 1=1 ";

            if (!string.IsNullOrEmpty(Workhours))
            {
                sql += " and Workhours like @Workhours ";
            }

            var list = await Db.QueryAsync<ProTemporaryWorkHours>(sql, new { Workhours = $"%{Workhours}%" });
            return list;

        }


        /// <summary>
        /// 获取总工时
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetWorkhours()
        {
            double count = 0;
            try
            {
                string sql = "select * from ProTemporaryWorkHours";
                var list = await Db.QueryAllAsync<ProTemporaryWorkHours>(sql);

                foreach (var item in list)
                {
                    count += Convert.ToDouble(item.Workhours);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return count.ToString();
        }





        /// <summary>
        /// 事物修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(ProTemporaryWorkHours model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }



    
}
